package com.android.alarmbuy;

import android.app.Application;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.util.Log;

import com.lzy.okgo.OkGo;

public class AlarmApplication extends Application {

    public static MediaPlayer mediaPlayer;
    public static boolean alarmStop = false;
    private static AudioManager mAudioManager;
    private static int maxVolume;
    private static int currentVolume;
    public static Context context;
    public static String response = "0";

    @Override
    public void onCreate() {
        super.onCreate();
        context = this;
        OkGo.getInstance().init(this);
        mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
        maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    }

    public static void playSound() {
        Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
        Log.d("leihuiming", "uri = " + uri);
        currentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        mediaPlayer = MediaPlayer.create(context, uri);
        mediaPlayer.setLooping(true);
        if (!mediaPlayer.isPlaying()) {
            Log.d("leihuiming", "maxVolume = " + maxVolume);
            setStreamVolume(maxVolume);
            mediaPlayer.start();
        }
    }

    public static void stopSound() {
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            //currentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
            Log.d("leihuiming", "currentVolume = " + currentVolume);
            setStreamVolume(currentVolume);
            mediaPlayer.pause();
            mediaPlayer.seekTo(0);
        }
    }

    public static void setStreamVolume(int index) {
        mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, index, AudioManager.FLAG_PLAY_SOUND);
    }
}
